package sort;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2022/4/15
 * \* Time: 9:26
 * \* To change this template use File | Settings | File Templates.
 * \* Description:冒泡排序：两两交换相邻记录的关键字，如果反序则交换，知道没有反序的结果为止
 * \
 */
public class BubbleSort {
    public static void bubbleSort1(int []arr){
        for(int i = 0; i < arr.length; i++){
            for(int j = i+1; j<arr.length;j++){
                if(arr[i] > arr[j]){
                    int tmp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = tmp;
                }
            }
        }
    }

    public static void bubbleSort2(int []arr){
        for(int i =0; i<arr.length;i++){
            boolean flg = false;
            for(int j = 0; j < arr.length - 1-i; j++){
                if(arr[j] > arr[j+1]){
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] =tmp;
                    flg = true;
                }
            }
            if(flg == false){
                break;
            }
        }
    }

    public static void main(String[] args) {
        int[] array = {2,3,1,4,6,3,4,77,6};
        bubbleSort3(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }


    }


    public static void bubbleSort3(int[] arr){
        for (int i =0;i <arr.length;i++){
            boolean flg = true;
            for (int j = 0; j < arr.length-i-1; j++) {
                if(arr[j] > arr[j+1]){
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                    flg = false;
                }

            }
            if(flg == true){
                break;
            }
        }
    }
}
